package net.seabook.games.tetris.model;


public class RZBlock extends Piece {

	public RZBlock(int x, int y) {
		
		for (int i = 0; i < 2; i++) {
			Tile tile = new Tile(x + i * Tile.WIDTH, y);
			getBlocks().add(tile);
		}
		
		for (int j = 0; j < 2; j++) {
			Tile tile = new Tile(x + (j + 1) * Tile.WIDTH, y - Tile.HEIGHT);
			getBlocks().add(tile);
		}
	}

	@Override
	public void rotate() {
		switch (getrDirection()) {
		case LEFT:
			if (left2Up())	setrDirection(RotateDirection.UP); 
			break;
		case UP:
			if(up2Left())	setrDirection(RotateDirection.LEFT);
			break;
		default:
			break;
		}
	}

	private boolean left2Up() {
		return actualMoveTiles(1);
	}
	
	private boolean up2Left() {
		return actualMoveTiles(-1);
	}
	
	private boolean actualMoveTiles(int i) {
		Tile tile1 = this.getBlocks().get(0);
		int x1 = tile1.getX() + 2 * i * Tile.WIDTH;
		int y1 = tile1.getY() + i * Tile.HEIGHT;
		Tile tile2 = this.getBlocks().get(1);
		int x2 = tile2.getX() + i * Tile.WIDTH;
		int y2 = tile2.getY();
		Tile tile3 = this.getBlocks().get(2);
		int x3 = tile3.getX();
		int y3 = tile3.getY() + i * Tile.HEIGHT;
		Tile tile4 = this.getBlocks().get(3);
		int x4 = tile4.getX() - i * Tile.WIDTH;
		int y4 = tile4.getY();
		
		if (isRotateOutofBound(x1) || isRotateOutofBound(x2) || isRotateOutofBound(x3) || isRotateOutofBound(x4)) {
			return false;
		}
		
		tile1.setTilePosition(x1, y1);
		tile2.setTilePosition(x2, y2);
		tile3.setTilePosition(x3, y3);
		tile4.setTilePosition(x4, y4);
		return true;
	}

}
